#include "MainScene.h"
#include "editor-support/cocostudio/ActionTimeline/CSLoader.h"
#include "cocos_util/ResourceUtil.h"
#include "MoveByCircle.h"

USING_NS_CC;
using namespace elloop;

const std::string MainScene::MINUTE_NODE("minutesNode");
const std::string MainScene::SECOND_NODE("secondsNode");

Scene* MainScene::createScene() {
    auto scene = Scene::create();
    auto layer = MainScene::create();
    scene->addChild(layer);
    return scene;
}

bool MainScene::init() {
    if ( !Layer::init() ) {
        return false;
    }

    auto visibleSize    = Director::getInstance()->getVisibleSize();
    auto origin         = Director::getInstance()->getVisibleOrigin();

    _root = LayoutPtr(CSLoader::createNode("MainScene.csb"));
    if ( _root ) {
        _root->setContentSize(visibleSize);
        auto pos = _root->getPosition();
        //_root->setPosition(pos.x + 100, pos.y);

        _root->setColor(ccc3(255, 255, 0));
        addChild(_root);
        _particle = _root->getChildByName("mParticle");
        CCASSERT(_particle != nullptr, "no particle called mParticle");
    }

    auto listener = EventListenerKeyboard::create();
    listener->onKeyPressed = [this](EventKeyboard::KeyCode code, Event* event) {
        if ( EventKeyboard::KeyCode::KEY_K == code ) {
            //testCustomActions();
            //this->moveParticle();
            this->moveCircles();
        }
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    //_particle->setPosition(Vec2(_particle->getPositionX() + 50, _particle->getPositionY() - 100));

    _particle->setVisible(false);
    _particle->removeFromParentAndCleanup(false);
    //auto particlePos = _particle->getPosition();
    /* auto draw = DrawNode::create();
    auto drawPos = particlePos;
    drawPos.subtract(Vec2(100, 100));
    draw->setPosition(drawPos);

    auto radius = particlePos.distance(drawPos);
    draw->drawDot(Vec2(0, 0), 2, ccc4f(1, 0, 0, 1));
    draw->drawCircle(Vec2(0, 0), radius, 180, 50, false, ccc4f(1, 0, 0, 1));
    _root->addChild(draw);*/



    auto big = DrawNode::create();
    _centerPos = Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
    big->setPosition(_centerPos);
    _root->addChild(big);
    auto bigRadius = visibleSize.width / 2 - 5;
    big->drawCircle(Vec2::ZERO, bigRadius, 180, 50, false,
        ccc4f(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 1));
    big->drawDot(Vec2::ZERO, 2, ccc4f(1, 0, 0, 1));
    big->setName("big");

    auto minNode = DrawNode::create();
    _minPos = _centerPos;
    auto minRadius = bigRadius / 2;
    _minPos.subtract(Vec2(bigRadius / 2, 0));
    minNode->setPosition(_minPos);
    minNode->drawCircle(Vec2::ZERO, minRadius, 180, 50, false,
        ccc4f(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 1));
    minNode->drawDot(Vec2::ZERO, 2, ccc4f(1, 0, 0, 1));
    _root->addChild(minNode);
    minNode->setName(MINUTE_NODE);

    auto secNode = DrawNode::create();
    _secPos = Vec2::ZERO;
    _secPos.subtract(Vec2(minRadius / 2, 0));
    auto _secRadius = minRadius / 2;
    secNode->setPosition(_secPos);
    _particle->setPosition(Vec2::ZERO);
    //_particle->setVisible(true);
    secNode->addChild(_particle);
    secNode->drawCircle(Vec2::ZERO, _secRadius, 180, 50, false,
        ccc4f(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 1));
    secNode->drawDot(Vec2::ZERO, 2, ccc4f(1, 0, 0, 1));
    minNode->addChild(secNode);
    secNode->setName(SECOND_NODE);

    const float PI = 3.1415926;
    float angle(PI / 4);
    CCLOG("tan(angle): %lf\n", tan(angle));
    CCLOG("sin(angle): %lf\n", sin(angle));
    CCLOG("arctan(1): %lf, pi/4 = %lf\n", atan(1), PI / 4);
    CCLOG("asin(0.707107): %lf\n", asin(0.707107));
    return true;
}

void MainScene::menuCloseCallback(Ref* pSender) {
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void MainScene::moveParticle() {
    auto origin = _particle->getPosition();
    MoveByCircle * circleAction = MoveByCircle::create(3.0, Vec2(origin.x - 100, origin.y - 100));
    RepeatForever* moveByCircle = RepeatForever::create(circleAction);
    _particle->runAction(moveByCircle);
    //_particle->runAction(circleAction);
}

void MainScene::moveCircles() {
    if ( _root ) {
        auto mid = _root->getChildByName(MINUTE_NODE);
        if ( mid ) {
            auto circleAct = MoveByCircle::create(60, _centerPos);
            auto repeat = RepeatForever::create(circleAct);
            mid->runAction(repeat);

            auto secNode = mid->getChildByName(SECOND_NODE);
            if ( secNode ) {
                auto centerUpdater = [mid]() -> Vec2 {
                    return mid->getPosition();
                };
                auto circel2 = MoveByCircle::create(1, Vec2::ZERO,
                    false, centerUpdater);
                auto repeat = RepeatForever::create(circel2);
                secNode->runAction(repeat);
            }
        }
    }
}

